<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/21
 * Time: 20:17
 */

namespace ticket\modules\backend\controllers;


use ticket\libs\Tools;
use ticket\modules\backend\models\TicketType;
use Yii;

/***  门票所属分类
 * Class TicketTypeController
 * @package ticket\modules\backend\controllers
 */
class TicketTypeController extends BasicController
{
    public function actionTicketTypeList()
    {
        $get = Yii::$app->request->get();
        $page = isset($get['page'])?$get['page']:1;
        $pageSize = isset($get['size'])?$get['size']:10;

        $searchModel = TicketType::find();
        $count = $searchModel->count();
        $typeList = $searchModel->offset($pageSize * ($page - 1))->orderBy(['id' => SORT_DESC])->limit($pageSize)->asArray()->all();

        return Tools::showRes(0,'success',[
            'typeList' => $typeList,
            'page' => $page,
            'pageSize' => $pageSize,
            'count' => $count,
        ]);

    }



    /*** 添加门票类别
     * @return false|string
     */
    public function actionAddTicketType()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $aid = $this->appId;
            $typeModel = new TicketType();
            if (!$typeModel->addType($aid,$post)){
                if ($typeModel->hasErrors()){
                    return Tools::showRes(99,$typeModel->getErrors());
                }else{
                    return Tools::showRes(99,'添加失败');
                }
            }

            return Tools::showRes(0,'添加成功');
        }
    }


    /*** 修改门票类别
     * @return false|string
     */
    public function actionModTicketType()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $typeModel = new TicketType();
            $id = isset($post['id'])?$post['id']:'';
            if (!$typeModel->modType($id,$post)){
                if ($typeModel->hasErrors()){
                    return Tools::showRes(99,$typeModel->getErrors());
                }else{
                    return Tools::showRes(99,'修改失败');
                }
            }

            return Tools::showRes(0,'修改成功');

        }
    }

    /*** 获取门票类别
     * @return false|string
     */
    public function actionGetTicketType()
    {
        $get = Yii::$app->request->get();
        $aid = $this->appId;
        $id = isset($get['id'])?$get['id']:'';
        if  (empty($id)){
            return Tools::showRes(99,'参数有误');
        }
        $typeModel = new TicketType();
        $attrInfo = $typeModel->getTypeInfo($id);
        if (!$attrInfo){
            $attrInfo = [];
        }
        return Tools::showRes(0,'success',[
            'attrInfo' => $attrInfo,
        ]);
    }



    /*** 删除门票类别
     * @return false|string
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelTicketType()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = isset($post['id'])?$post['id']:'';
            if  (empty($id)){
                return Tools::showRes(99,'参数有误');
            }
            $typeModel = new TicketType();
            if ($typeModel->delTicketType($id)){
                return Tools::showRes(0,'删除成功');
            }
            return Tools::showRes(99,'删除失败');
        }
    }




}